package com.sulent.frame.web.controller.test;



import cn.hutool.core.io.resource.ResourceUtil;
import com.sulent.frame.file.FileServer;
import com.sulent.frame.file.dto.FileVo;
import com.sulent.frame.file.impl.FTPFileServer;
import com.sulent.frame.file.impl.MinioFileServer;
import com.sulent.frame.file.impl.SftpFileServer;
import com.sulent.frame.support.exception.BusinessException;
import com.sulent.frame.web.template.FtpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * @ClassName FileSysController
 * @Description: TODO
 * @Author Submerge.
 * @Since 2024/1/11 18:01
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/file/sys/v1")
public class FileSysController {


    @GetMapping("/remove")
    public String remove(){

        return "success";
    }

    @GetMapping("/fileList")
    public List<FileVo> fileList(String path){
//        FileServer fileServer = new FTPFileServer("192.168.138.1", 21, "ftpuser", "Su8023@bb", "gbk");
//        FileServer fileServer = new FTPFileServer("172.16.48.16", 21, "dgftp", "Gd@123456", "gbk");
//        FileServer fileServer = new MinioFileServer("http://172.16.32.23:30021", "jIWdnS7x6g9dWMAY", "P5qsVxVTUmeNPMMQGfN5Ly9g87rDDW3v", "datacenter");
//        FileServer fileServer = new MinioFileServer("http://172.16.0.16:9000/", "rcmL8t0EfcmaGrEQ", "0CDZzmRFlqLaRnoufn177wycXlin3lt1", "ai-model");
//        FileServer fileServer = new MinioFileServer("http://172.16.0.16:9000/", "rcmL8t0EfcmaGrEQ", "0CDZzmRFlqLaRnoufn177wycXlin3lt1", "ai-model");
//        FileServer fileServer = new FTPFileServer("192.168.138.100", 22, "root", "centos", "gbk");
//        FileServer fileServer = new SftpFileServer("192.168.138.100", 22, "root", "centos", 10000);
        FileServer fileServer = new MinioFileServer("http://172.16.32.23:30021", "jIWdnS7x6g9dWMAY", "P5qsVxVTUmeNPMMQGfN5Ly9g87rDDW3v", "datacenter");
        List<FileVo> fileVos = fileServer.listFiles(path);
        return fileVos;
    }

    @GetMapping("/downloadFile")
    public String downloadFile(@RequestParam String filePath){
//        FileServer fileServer = new FTPFileServer("192.168.138.1", 21, "ftpuser", "Su8023@bb", "utf-8");
//        FileServer fileServer = new FTPFileServer("172.16.48.16", 21, "dgftp", "Gd@123456", "gbk");
//        FileServer fileServer = new MinioFileServer("http://172.16.32.23:30021", "jIWdnS7x6g9dWMAY", "P5qsVxVTUmeNPMMQGfN5Ly9g87rDDW3v", "datacenter");
//        FileServer fileServer = new MinioFileServer("http://172.16.0.16:9000/", "rcmL8t0EfcmaGrEQ", "0CDZzmRFlqLaRnoufn177wycXlin3lt1", "ai-model");
//        FileServer fileServer = new MinioFileServer("http://172.16.0.16:9000/", "rcmL8t0EfcmaGrEQ", "0CDZzmRFlqLaRnoufn177wycXlin3lt1", "ai-model");
//        FileServer fileServer = new FTPFileServer("192.168.138.100", 22, "root", "centos", "gbk");
//        FileServer fileServer = new SftpFileServer("192.168.138.100", 22, "root", "centos", 10000);
//        FileServer fileServer = new FTPFileServer("172.16.48.16", 21, "dgftp", "Gd@123456", "gbk");
        FileServer fileServer = new SftpFileServer("192.168.138.100", 22, "root", "centos", 10000);
        File file = fileServer.downloadFile(filePath, "temp-file");
        if (file!=null && file.isFile()){
            return "下载成功";
        }else {
            return "下载失败";
        }
    }

    @GetMapping("/pageFiles")
    public List<FileVo> uploadFile(String path, String startAfter){
//        MinioFileServer fileServer =  new MinioFileServer("http://172.16.32.23:30021", "jIWdnS7x6g9dWMAY", "P5qsVxVTUmeNPMMQGfN5Ly9g87rDDW3v", "datacenter");
        MinioFileServer fileServer =  new MinioFileServer("http://172.16.0.16:9000/", "rcmL8t0EfcmaGrEQ", "0CDZzmRFlqLaRnoufn177wycXlin3lt1", "ai-model");
        return fileServer.pageFiles(path, startAfter);
    }


    @GetMapping("/download")
    public String download(@RequestParam String filePath, HttpServletResponse response) throws IOException {
//        FileServer fileServer = new FTPFileServer("192.168.138.1", 21, "ftpuser", "Su8023@bb", "utf-8");
//        FileServer fileServer = new FTPFileServer("172.16.48.16", 21, "dgftp", "Gd@123456", "gbk");
//        FileServer fileServer = new MinioFileServer("http://172.16.32.23:30021", "jIWdnS7x6g9dWMAY", "P5qsVxVTUmeNPMMQGfN5Ly9g87rDDW3v", "datacenter");
//        FileServer fileServer = new MinioFileServer("http://172.16.0.16:9000/", "rcmL8t0EfcmaGrEQ", "0CDZzmRFlqLaRnoufn177wycXlin3lt1", "ai-model");
        FileServer fileServer = new MinioFileServer("http://172.16.0.16:9000/", "rcmL8t0EfcmaGrEQ", "0CDZzmRFlqLaRnoufn177wycXlin3lt1", "ai-model");
//        FileServer fileServer = new FTPFileServer("192.168.138.100", 22, "root", "centos", "gbk");
//        FileServer fileServer = new SftpFileServer("192.168.138.100", 22, "root", "centos", 10000);
//        FileServer fileServer = new FTPFileServer("172.16.48.16", 21, "dgftp", "Gd@123456", "gbk");
        fileServer.downloadFile(filePath, "temp-file"+"\\"+filePath);
        File file = new File("temp-file"+"\\"+filePath);
        response.setContentType("application/pdf");
        // 除了 "attachment" 外，Content-Disposition 还可以设置为 "inline"，这表示内容应该在浏览器窗口中直接显示，而不是作为附件下载。
        response.setHeader("Content-Disposition", "inline; filename=\"" + "test.pdf" + "\"");
//        response.setContentLength((int) file.length());
        IOUtils.copy(new FileInputStream(file),response.getOutputStream());
        if (file!=null && file.isFile()){
            return "下载成功";
        }else {
            return "下载失败";
        }
    }


    @GetMapping("/example/template")
    public void viewTemplate(HttpServletResponse response) {
        String fileName = "文件模板.xlsx";
        Resource resource = new ClassPathResource("doc/"+fileName);
        try (InputStream inputStream = resource.getInputStream();
             OutputStream outputStream = response.getOutputStream()) {
            response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()));
            response.setContentType("application/octet-stream;charset=UTF-8");
            IOUtils.copy(inputStream, outputStream);
        } catch (IOException e) {
            log.error("文件模板下载异常：{}",e.getMessage(),e);
            throw new BusinessException("文件模板下载异常:"+e.getMessage());
        }
    }

    @GetMapping("/example/resolveFileAsObject")
    public void resolveFileAsObject(HttpServletResponse response) {
        String fileName = "overview.json";
        String fileJson = ResourceUtil.readStr("doc/"+fileName, StandardCharsets.UTF_8);
        //..parseObject
    }



}
